package com.cloudcc.mobile.parser;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.cloudcc.mobile.entity.ChatUser;
import com.cloudcc.mobile.entity.ChatUserInfo;
import com.cloudcc.mobile.util.Json;
import com.cloudcc.mobile.util.Tools;

public class ChatUserParser {
	/**
	 * 解析查询的chatuser
	 * @param jsonString
	 * @return
	 */
	public  static ChatUserInfo getImageInfo(String jsonString){
		ChatUserInfo userinfo=new ChatUserInfo();
		try {
			JSONObject obj=new JSONObject(jsonString);
			userinfo.setResult(obj.getBoolean("result"));
			userinfo.setReturnCode(obj.getString("returnCode"));
			userinfo.setReturnInfo(obj.getString("returnInfo"));
			JSONObject chatuser=obj.getJSONObject("data");
			ChatUser user=(ChatUser) Json.toObject(chatuser.toString(), ChatUser.class);
			userinfo.setChatuser(user);
		} catch (JSONException e) {
			Tools.handle(e);
		}
		return userinfo;
	}
	/**
	 * 解析查询的chatuser
	 * @param jsonString
	 * @return
	 */
	public  static ChatUserInfo getUserInfo(String jsonString){
		ChatUserInfo userinfo=new ChatUserInfo();
		try {
			JSONObject obj=new JSONObject(jsonString);
			userinfo.setResult(obj.getBoolean("result"));
			userinfo.setReturnCode(obj.getString("returnCode"));
			userinfo.setReturnInfo(obj.getString("returnInfo"));
			if(obj.getBoolean("result")){
				JSONObject chatuser=obj.getJSONObject("data");
				ChatUser user=new ChatUser();
				user.set_id(chatuser.getString("_id"));
				if(chatuser.has("aboutme")){
					user.setAboutme(chatuser.getString("aboutme"));
				}else{
					user.setAboutme("");
				}
				if(chatuser.has("address")){
					user.setAddress(chatuser.getString("address"));
				}else{
					user.setAddress("");
				}
				user.setCommentednum(chatuser.getString("commentednum"));
				user.setCreateDate(chatuser.getString("createDate"));
				user.setEmail(chatuser.getString("email"));
				user.setFeedandcomnum(chatuser.getString("feedandcomnum"));
				user.setLastModifyDate(chatuser.getString("lastModifyDate"));
				user.setMobile(chatuser.getString("mobile"));
				user.setName(chatuser.getString("name"));
				user.setPhone(chatuser.getString("phone"));
				user.setPraisenum(chatuser.getString("praisenum"));
				user.setTitle(chatuser.getString("title"));
				user.setUserid(chatuser.getString("userid"));
				if(chatuser.has("img")){
					JSONArray image=chatuser.getJSONArray("img");
					byte[] topimg=new byte[]{};
					for (int i = 0; i < image.length(); i++) {
						topimg[i]=(Byte) image.get(i);
					}
					user.setImg(topimg);
				}else{
					user.setImg(null);
				}
				userinfo.setChatuser(user);
			}
		} catch (JSONException e) {
			Tools.handle(e);
		}

		return userinfo;
	}

}
